package com.example.plat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.plat.enity.Activity;
import com.example.plat.enity.Schedule;
import com.example.plat.exception.ScheduleException;
import com.example.plat.mapper.ActivityMapper;
import com.example.plat.mapper.ScheduleMapper;
import com.example.plat.service.ScheduleService;
import com.example.plat.util.TimeFormateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Comparator;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper mapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public IPage<Activity> getMyScheduleCard(String sponsor,Integer pageNum,Integer pageSize) {
        Page<Activity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Activity::getPublisher,sponsor);

        activityMapper.selectPage(page,lqw);
        return page;
    }

    @Override
    public List<Activity> getJoinScheduleCard(Long userId, Integer pageNum, Integer pageSize) {
//        Page<Activity> page = new Page<>(pageNum, pageSize);
//        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<>();
        List<Activity> list = mapper.getJoinScheduleCard(userId, pageNum, pageSize);
        list.sort(new Comparator<Activity>() {
            @Override
            public int compare(Activity o1, Activity o2) {
                String u = o1.getTime();
                String v = o2.getTime();
                try {
                    return TimeFormateUtil.judgeGreat(u,v)?-1:1;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return list;
    }

    @Override
    public Activity getOneCard(Long actId) {
        return mapper.getOneCard(actId);
    }

    @Override
    public List<Schedule> getScheduleDetail(Long actId) {
        return mapper.getScheduleDetail(actId);
    }

    @Transactional
    @Override
    public void addSchedule(List<Schedule> schedule) throws ScheduleException {
        try {
            for(Schedule s : schedule){
                mapper.insert(s);
            }
        }catch (Exception e){
            throw new ScheduleException("添加日程失败！");
        }
    }

    @Transactional
    @Override
    public void updateSchedule(List<Schedule> schedule) throws ScheduleException {
        try {
            //先删除所有日程
            LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Schedule::getActId,schedule.get(0).getActId());
            mapper.delete(lqw);

            //添加日程
            for(Schedule s : schedule){
                mapper.insert(s);
            }
        }catch (Exception e){
            throw new ScheduleException("修改日程失败！");
        }
    }
}
